import { CustomerService } from 'src/app/@core/services/customer/customer.service';
import { Component, ElementRef, Input, ViewChild } from '@angular/core';
import { DataTableComponent, TableWidthConfig, TreeComponent } from 'ng-devui';

@Component({
  selector: 'app-select-customer',
  templateUrl: './select-customer.component.html',
  styleUrls: ['./select-customer.component.scss']
})
export class SelectCustomerComponent {
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  displayTreeData: any[];
  treeData: any;
  tableheight = '230px';
  selectData: any;
  searchName: ''
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'radio',
        header: '选择',
        fieldType: 'radio'
      },
      {
        field: 'code',
        header: '客户编码',
        fieldType: 'text',

      },
      {
        field: 'name',
        header: '客户名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '客户简称',
        fieldType: 'text',
      }
    ]
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '50px'
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    }
  ]
  filter = {
    PageSize: 20,
    PageNumber: 1,
    SearchTerm: '',
    SearchStatus: '',
    SearchCustomerType: '',
    SalesmanId: '',
    CustomerClassId: '',
  };
  @ViewChild('basicTree', { static: true }) basicTree!: TreeComponent;
  @ViewChild('radioInput') radioInput: ElementRef;
  customerTypeId: any;
  tabledata: any;
  searchValue: any;
  filteredTreeNodes: any[];
  searchDepartmentValue: any;
  constructor(private CustomerService: CustomerService) { }
  ngOnInit(): void {
    this.CustomerService.GetPagedCustomerClassWithSub(1, 9999).subscribe((data: any) => {
      this.treeData = data
      this.displayTreeData = [{ customerClassName: '全部', customerClassId: null }, ...this.treeData];
      this.CustomerService.GetPagedCustomer(this.filter).subscribe((data: any) => {
        this.tabledata = data.items
        if (this.radioInput) {
          const inputElement = this.radioInput.nativeElement as HTMLInputElement;
          inputElement.checked = false; // 修改 input 元素的值
        }
        this.selectData = null
      })
    })

  }
  afterTreeInit($event: any) {
    if ($event["1"]) {
      $event["1"].data.isActive = true
    }
  }//树形菜单初始化
  nodeSelected($event: any) {
    this.customerTypeId = $event.data.originItem.customerClassId;
    this.filter.CustomerClassId = this.customerTypeId
    this.CustomerService.GetPagedCustomer(this.filter).subscribe((data: any) => {
      this.tabledata = data.items
      if (this.radioInput) {
        const inputElement = this.radioInput.nativeElement as HTMLInputElement;
        inputElement.checked = false; // 修改 input 元素的值
      }
      this.selectData = null
    })
  }//树形菜单点击事件
  searchDepartment($event: any) {
    this.searchDepartmentValue = $event.target.value
    if (this.searchDepartmentValue === '') {
      this.displayTreeData = [{ customerClassName: '全部', customerClassId: null }, ...this.treeData];
    }else{
      this.filteredTreeNodes = this.filterCategories(this.treeData, this.searchDepartmentValue);
      this.displayTreeData = this.filteredTreeNodes
      this.displayTreeData = this.displayTreeData.map(this.setOpenTrue);
    }
  }//搜索部门
  filterCategories(departments: any[], keyword: string): any[] {
    const result: any[] = [];
    for (const department of departments) {
      if (department.customerClassName.includes(keyword)) {
        result.push(department);
      }
      if (department.subCustomerClasses.length > 0) {
        const filteredSubCategories = this.filterCategories(department.subCustomerClasses, keyword);
        if (filteredSubCategories.length > 0) {
          result.push(...filteredSubCategories);
        }
      }
    }
    return result;
  }
  setOpenTrue = (node: { open: boolean; subCustomerClasses: any[]; }) => {
    // 为当前节点添加 open: true 属性
    node.open = true;
    // 如果当前节点有子节点，递归处理子节点
    if (Array.isArray(node.subCustomerClasses)) {
      node.subCustomerClasses = node.subCustomerClasses.map(this.setOpenTrue);
    }
    return node;
  }
  radio($event: any, rowItem: any) {
    if ($event.target.checked) {
      this.isSelected = true;
      this.selectData = rowItem
    }
  }
  search() {
    this.CustomerService.GetPagedCustomer(this.filter).subscribe((data: any) => {
      this.tabledata = data.items
      if (this.radioInput) {
        const inputElement = this.radioInput.nativeElement as HTMLInputElement;
        inputElement.checked = false; // 修改 input 元素的值
      }
      this.selectData = null
    })
  }//搜索
  reset() {
    this.filter = {
      PageSize: 20,
      PageNumber: 1,
      SearchTerm: '',
      SearchStatus: '',
      SearchCustomerType: '',
      SalesmanId: '',
      CustomerClassId: '',
    }
    this.CustomerService.GetPagedCustomer(this.filter).subscribe((data: any) => {
      this.tabledata = data.items
      if (this.radioInput) {
        const inputElement = this.radioInput.nativeElement as HTMLInputElement;
        inputElement.checked = false; // 修改 input 元素的值
      }
      this.selectData = null
    })
  }//重置
  pageIndexChange() {
    this.CustomerService.GetPagedCustomer(this.filter).subscribe((data: any) => {
      this.tabledata = data.items
      if (this.radioInput) {
        const inputElement = this.radioInput.nativeElement as HTMLInputElement;
        inputElement.checked = false; // 修改 input 元素的值
      }
      this.selectData = null
    })
  }
  pageSizeChange() {
    this.CustomerService.GetPagedCustomer(this.filter).subscribe((data: any) => {
      this.tabledata = data.items
      if (this.radioInput) {
        const inputElement = this.radioInput.nativeElement as HTMLInputElement;
        inputElement.checked = false; // 修改 input 元素的值
      }
      this.selectData = null
    })
  }
}
